﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using GWEB.WebBase.Data;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;

namespace GWEB.WebFrame.Sys
{
    public class UserController : PageInfoBase
    {        
        [ActionFilter(IsLogin = true)]
        public ActionResult Index(String id, String keyCode)
        {          
            String sql = String.Format("select t1.srolecode,t1.srolename,(case when t2.iuserid is null then 0 else 1 end) state from tb_sys_role t1 left join tb_sys_userrole t2 on t1.srolecode=t2.srolecode and t2.iuserid={0} order by t1.srolecode desc", id);
            ViewBag.UserRole= BasePage.bd.ExecuteTable(sql);
            sql = "select id,name from cu_sys_league where zt=1";
            ViewBag.League = BasePage.bd.ExecuteTable(sql);
            return Load("User", "Sys/User", id, keyCode); 
        }
        protected override void FieldAddInit(DataRow data) { data["bstopflag"] = 0; data["UserType"] = 0; }
        protected override SqlState SqlExecute(BaseData bd, String id, Dictionary<String, Object> data)
        {
            String rolecodes = data["rolecodes"].ToString();
            data.Remove("rolecodes");
            //增加时取序列，修改是不取序列，否则会引发错误		
            String[] sql = new String[3];
            sql[0] = BaseData.MapToSql("tb_sys_users", "iuserid", id, data);
            sql[1] = "delete tb_sys_userrole where iuserid=[iuserid]";
            sql[2] = String.Format("insert into tb_sys_userrole (iuserid,srolecode) select [iuserid],srolecode from tb_sys_role where srolecode in ({0})", rolecodes);
            bool t = bd.ExecuteSQL(sql, -1, "tb_sys_users", "iuserid", id);
            return t ? SqlState.True : SqlState.False;
        }
        protected override String Validate(Dictionary<String, Object> data)
        {
            String userName = data["susername"].ToString();
            if (userName.Length > 20) return Check.CheckPromt("用户名长度超过20！");         
            String reg = "[a-zA-Z0-9~!@#$%^&*_-]|[\u4e00-\u9fa5]*";
            if (!Regex.IsMatch(userName, reg)) return Check.CheckPromt("用户名出现非法字符！");

            if (!data["mpassword"].ToString().Equals(""))
            {
                String newpass = data["mpassword"].ToString();
                int UserPwdType = Config.UserPwdLevel;
                if ((UserPwdType == 1 || UserPwdType == 2) && newpass.Length < 8)
                {
                    return Check.CheckPromt("新密码必须8位以上！");
                }
                if (UserPwdType == 2)
                {
                    reg = "^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*])[\\da-zA-Z~!@#$%^&*]{8,}$";
                    if (!Regex.IsMatch(newpass, reg))
                    {
                        return Check.CheckPromt("新密码需由字母、数字、特殊字符(~!@#$%^&*)组成！");
                    }
                }
                String code = data["susername"].ToString().ToLower() + data["mpassword"].ToString();
                code = Fun.MD5(code);
                code = Fun.MD5(code.Substring(10));
                data["mpassword"]=code;
            }
            else
                data.Remove("mpassword");            
            return "";
        }
    }
}